package com.jiejie.service.impl;


import com.jiejie.service.OrderRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class OrderRedisServiceImpl implements OrderRedisService {
    @Autowired
    private RedisTemplate redisTemplate;
    /*@Override
    public boolean saveOrder2Redis(MyOrder order) {
        String key ="unPayedOrder_"+order.getCustomerId() + "_"+order.getOrdedrId();  // unPayedOrder_用户id_订单id
        redisTemplate.opsForValue().set(key,order,10, TimeUnit.SECONDS);
        return true;
    }

    @Override
    public MyOrder getOrderByIdFromRedis(Integer customerId, Integer orderId) {
        String key ="unPayedOrder_"+customerId + "_"+orderId;  // unPayedOrder_用户id_订单id

        MyOrder order =(MyOrder) redisTemplate.opsForValue().get(key);
        System.out.println("从redis中获得该订单：" + order);
        if(order !=null){
            boolean deleteResult = redisTemplate.delete(key);
            if(deleteResult)
            System.out.println("获得该订单，并从redis中移除");
        }
        return order;
    }*/

    @Override
    public boolean saveUnPayedMessage2Redis(Integer customerId, Integer orderId) {
        String key = "UnReadMsgOrderUnPayed_"+customerId+"_"+ orderId;
        redisTemplate.opsForValue().set(key,"用户"+customerId + "您有一个订单id="+orderId +"未支付的订单，还剩余1分钟，请快速去支付哦");
        return  true;
    }

    @Override
    public String getUnReadMessageFromRedis(Integer customerId, Integer orderId) {
        String key = "UnReadMsgOrderUnPayed_"+customerId+"_"+orderId;
        String value = (String) redisTemplate.opsForValue().get(key);
        System.out.println("从redis中获得未读的消息是：" + value);
        if(value !=null){
            boolean deleteResult = redisTemplate.delete(key);
            if(deleteResult)
                System.out.println("获得该未读的消息，并从redis中移除");
        }
        return value;
    }
}
